package adapter;

import java.util.ArrayList;

import objects.Money;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.viet_anh.managementmoney.R;

public class ListviewAdapterCustom extends BaseAdapter {

	private Context cxt;
	private ArrayList<Money> data;
	private LayoutInflater inflater;
	private int lastPosition = -1;

	public ListviewAdapterCustom(Context cxt, ArrayList<Money> data) {
		this.cxt = cxt;
		this.data = data;
		inflater = (LayoutInflater) this.cxt
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// inflater = activity.getLayoutInflater();

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		final String id = String.valueOf(data.get(position).getId());
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.listview_items, null);
			holder = new ViewHolder();
			holder.tvMoney = (TextView) convertView.findViewById(R.id.tvMoney);
			holder.tvDay = (TextView) convertView.findViewById(R.id.tvDay);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tvMoney.setText(String.valueOf(data.get(position).getMoney()));
		holder.tvDay.setText(data.get(position).getDay());

		holder.btnDelete = (ImageButton) convertView
				.findViewById(R.id.btnDelete);
		holder.btnDelete.setTag(position);
		
		//Animation like google+
		Animation animation = AnimationUtils.loadAnimation(cxt,
				(position > lastPosition) ? R.anim.up_from_bottom
						: R.anim.up_from_bottom);
		convertView.startAnimation(animation);
		lastPosition = position;
		/////
		
		holder.btnDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				DatabaseHandlerForApp db = new DatabaseHandlerForApp(cxt);
//				db.deleteMoneyById(id);
//				data.remove(Integer.parseInt(v.getTag().toString()));
//				ListviewAdapterCustom.this.notifyDataSetChanged();
//				Toast.makeText(cxt, "Delete successfully", Toast.LENGTH_SHORT)
//						.show();
				final Dialog dl = new Dialog(cxt,R.style.DialogSlideAnim);
				dl.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dl.setContentView(R.layout.dialog_comfirm);
				dl.show();
			}
		});
		return convertView;
	}

	static class ViewHolder {
		TextView tvMoney, tvDay;
		ImageButton btnDelete;
	}

}
